package com.aj.activity.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aj.activity.ChoosePhotoActivity;
import com.aj.activity.DirExplorerActivity;
import com.aj.activity.R;
import com.aj.utils.Contants;

/**
 * The Class MailGroupContactsAdapter.
 */
public class DirExplorerAdapter extends BaseAdapter {

	/** The context. */
	private Context context;

	/** The inflater. */
	private LayoutInflater inflater;

	private String zoneId;

	private String userId;

	/** The file map. */
	private LinkedHashMap<String, List<File>> fileMap = new LinkedHashMap<String, List<File>>();

	/** The index list. */
	private List<String> indexList = new ArrayList<String>();

	/**
	 * Gets the index list.
	 * 
	 * @return the index list
	 */
	public List<String> getIndexList() {
		return indexList;
	}

	/**
	 * Instantiates a new files explorer adapter.
	 * 
	 * @param context
	 *            the context
	 * @param fileMap
	 *            the file map
	 */
	public DirExplorerAdapter(Context context,
			LinkedHashMap<String, List<File>> fileMap, String zoneId,
			String userId) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.fileMap.clear();
		this.fileMap.putAll(fileMap);
		indexList.clear();
		for (String key : this.fileMap.keySet()) {
			indexList.add(key);
		}
		this.zoneId = zoneId;
		this.userId = userId;
		Collections.sort(indexList);
	}

	/**
	 * Gets the count.
	 * 
	 * @return the count
	 */
	@Override
	public int getCount() {
		return fileMap.size();
	}

	/**
	 * Gets the item.
	 * 
	 * @param position
	 *            the position
	 * @return the item
	 */
	@Override
	public Object getItem(int position) {
		return fileMap.get(indexList.get(position));
	}

	/**
	 * Gets the item id.
	 * 
	 * @param position
	 *            the position
	 * @return the item id
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Gets the view.
	 * 
	 * @param position
	 *            the position
	 * @param convertView
	 *            the convert view
	 * @param parent
	 *            the parent
	 * @return the view
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 使用View的对象itemView与R.layout.item关联
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_dir_explorer_list,
					null);
		}

		TextView dirPathView = (TextView) convertView
				.findViewById(R.id.dir_path);

		TextView searchResultView = (TextView) convertView
				.findViewById(R.id.search_result);

		String dirPath = indexList.get(position);
		dirPathView.setText(dirPath.substring(dirPath.lastIndexOf("/") + 1));

		searchResultView.setText("(" + ((List<File>) getItem(position)).size()
				+ ")");
		convertView.findViewById(R.id.dir_item).setTag(position);

		convertView.findViewById(R.id.dir_item).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(
								DirExplorerActivity.instance,
								ChoosePhotoActivity.class);
						intent.putExtra(Contants.DIR_PATH_PARAMS,
								indexList.get((Integer) v.getTag()));
						intent.putExtra(Contants.ZONE_ID_PARAMS, zoneId);
						intent.putExtra(Contants.USER_ID_PARAMS, userId);
						// 跳转到文件选择页面
						if (DirExplorerActivity.instance.getIntent()
								.getStringExtra(Contants.FILY_TYPE_PARAMS) != null
								&& DirExplorerActivity.instance
										.getIntent()
										.getStringExtra(
												Contants.FILY_TYPE_PARAMS)
										.trim().equals(Contants.TYPE_IMAGE)) {
							intent.putExtra(Contants.PAGEFLAG_STRING,
									Contants.TYPE_CHOOSEPHOTO);
						} else {
							intent.putExtra(Contants.PAGEFLAG_STRING,
									Contants.TYPE_CHOOSEVEDIO);
						}
						DirExplorerActivity.instance.startActivity(intent);
					}
				});

		return convertView;
	}

}
